package cn.raven.order.controller;

import cn.raven.basic.annotation.PreAuthorize;
import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.utils.LoginContext;
import cn.raven.order.dto.OrderConfirmationDto;
import cn.raven.order.dto.OrderDto;
import cn.raven.order.service.IOrderProductService;
import cn.raven.order.domain.OrderProduct;
import cn.raven.order.query.OrderProductQuery;
import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import cn.raven.org.domain.Employee;
import cn.raven.pet.domain.SearchMasterMsg;
import cn.raven.pet.query.SearchMasterMsgQuery;
import cn.raven.serve.domain.Product;
import cn.raven.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.function.LongConsumer;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;


    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    //@PreAuthorize(name = "保存orderProduct",sn = "orderProduct:save")
    public AjaxResult save(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    //@PreAuthorize(name = "删除orderProduct",sn = "orderProduct:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            orderProductService.cancel(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    //@PreAuthorize(name = "id查询orderProduct",sn = "orderProduct:id")
    public OrderProduct findById(@PathVariable("id")Long id)
    {
        return orderProductService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> findAll(){

        return orderProductService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    //@PreAuthorize(name = "orderProduct列表",sn = "orderProduct:list")
    public PageList<OrderProduct> queryData(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryData(query);
    }
    
    /* 
    * @Title: buy
    * @Description: 用户购买生成订单待处理
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 12:27  
    * @Parameters: [searchMasterMsg, request]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/buy")
    public AjaxResult buy(@RequestBody Product product, HttpServletRequest request) {
        try {
            orderProductService.buy(product,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * @Title: toHandle
    * @Description: 待服务
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 13:52
    * @Parameters: [query, request]
    * @Return cn.raven.basic.utils.PageList<cn.raven.order.domain.OrderProduct>
      */
    @PostMapping("/toHandle")
    //@PreAuthorize(name = "orderProduct列表",sn = "orderProduct:list")
    public PageList<OrderProduct> toHandle(@RequestBody OrderProductQuery query)
    {        // 1.设置服务订单状态为待服务
        query.setState(0);
        return orderProductService.queryData(query);
    }

    /**
    * @Title: accept
    * @Description: 待处理服务接单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 14:44
    * @Parameters: [dto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody OrderDto dto,HttpServletRequest request){
        try {
            orderProductService.accept(dto,request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
    
    /** 
    * @Title: confirm
    * @Description: 完成后确认订单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 15:40  
    * @Parameters: [dto]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderConfirmationDto dto){
        try {
            orderProductService.confirm(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage()).setSuccess(false);
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请骚后重试").setSuccess(false);
        }
    }
}
